package org.bambrikii.photobank.web.category.beans;

import org.bambrikii.photobank.business.managers.CategoryManager;
import org.bambrikii.photobank.business.managers.FileManager;
import org.bambrikii.photobank.model.Category;
import org.bambrikii.photobank.model.CategoryTypeEnum;
import org.bambrikii.photobank.model.File;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.model.Pager;
import org.bambrikii.site.base.struts2.core.WebException;
import org.bambrikii.site.base.struts2.core.crud.BaseCrudBean;

public class CategoryBean extends BaseCrudBean<Category, CategoryManager> {

	private CategoryManager categoryManager;
	private FileManager fileManager;

	public CategoryBean(CategoryManager categoryManager, FileManager fileManager) {
		this.categoryManager = categoryManager;
		this.fileManager = fileManager;
	}

	public CategoryManager getManager() {
		return categoryManager;
	}

	public Pager<Category> getCategories(Category parentCategory, int currPage) throws WebException {
		Pager<Category> pager = categoryManager.getCategoriesPager(parentCategory, parentCategory != null ? null
				: CategoryTypeEnum.MAIN, currPage);
		return pager;
	}

	public Category getCategory(Integer id) throws WebException {
		try {
			return categoryManager.getById(id);
		} catch (ManagerException ex) {
			throw new WebException("Cannot get category.", ex);
		}
	}

	public Pager<File> getFiles(Category category, int requestedPage) throws WebException {
		return fileManager.getFilesPager(category, requestedPage);
	}
}
